import store from "../store";
let countReducer = (init = 0, action) => {
	//  {type:"add",payload:"1"}
	//  {type:"jian",payload:"1"}
	switch (action.type) {
		case "add":
			return (init += action.payload);
		case "jian":
			return (init -= action.payload);
		//第一次调用方,必须返回初始值
		default:
			return init;
	}
};

export default countReducer;

//封装 action方法,返回一个action对象
//加
export function addAction(payload) {
	return {
		type: "add",
		payload: payload,
	};
}

//减
export function jianAction(payload) {
	return {
		type: "jian",
		payload,
	};
}

//  异步 + 10
export function asyncAddAction(payload) {
	store.dispatch((dispatch) => {
		return setTimeout(() => {
			dispatch(addAction(payload));
		}, 1000);
	});
}

// 异步 -10
export let asyncJianAction = (payload) => {
	store.dispatch((dispatch) => {
		return setTimeout(() => {
			dispatch(jianAction(payload));
		}, 1000);
	});
};
